package com.jubao.ejb.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.dao.UserDaoLocal;
import com.jubao.ejb.dao.UserDaoRemote;
import com.jubao.ejb.dao.util.DaoConstant;
import com.jubao.ejb.entity.User;
import com.jubao.ejb.entity.UserWebsite;

@Stateless
@LocalBinding(jndiBinding = DaoConstant.UserDaoLocalJndiName)
@RemoteBinding(jndiBinding = DaoConstant.UserDaoRemoteJndiName)
public class UserDaoImpl implements UserDaoLocal, UserDaoRemote {
	static Logger logger = Logger.getLogger(StoreDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -7321946462065370049L;

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public void deleteUserBySysId(long sysId) {
		// TODO Auto-generated method stub

	}

	public void deleteUserByUserId(String userId) {
		// TODO Auto-generated method stub

	}

	public User find(long sysId) {
		User result = em.find(User.class, sysId);
		return result;
	}

	public User find(String id) {
		Query q = em.createNamedQuery("UserDao.eql.findUserById").setParameter("id", id).setMaxResults(1);
		//.setHint("org.hibernate.cacheRegion", "/entity/Category/Single")
		List<User> users = q.getResultList();
		User result = null;
		if (users != null && users.size() > 0) {
			result = users.get(0);
		}
		return result;
	}

	public User save(User user) {
		User c = em.merge(user);
		return c;
	}

}
